﻿using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Xml;

namespace ConsoleTestXmlJsonTransfer
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @".\Struct\xml文件demo.xml";
            string xmlContent = string.Empty;
            using (StreamReader sr = new StreamReader(fileName))
            {
                xmlContent = sr.ReadToEnd();
            }

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlContent);
            {
                
                string jsonString = XmlJsonConvertor.XMLConvertToJsonStr(xmlDocument);
                Console.WriteLine("xml对象转json字符串：");
                Console.WriteLine(jsonString);
            }

            {
                string jsonString2 = XmlJsonConvertor.XMLConvertToJsonStr(xmlContent);
                Console.WriteLine("xml字符串转json字符串：");
                Console.WriteLine(jsonString2);
            }

            //string jsonFilName = @"D:\liuhuairiWorkSpace\Project\json-xml-mutual-transfer\JsonXmlMutualTransfer\bin\Debug\netcoreapp3.1\Struct\json文件demo.json";
            string jsonFilName = @".\Struct\json文件demo.json";
            string jsonContent = string.Empty;
            using (StreamReader sr = new StreamReader(jsonFilName))
            {
                jsonContent = sr.ReadToEnd();
            }

            {
                string xmlResult = XmlJsonConvertor.JsonConvertToXMLStr(jsonContent);
                Console.WriteLine("json字符串转Xml字符串：");
                Console.WriteLine(xmlResult);
            }

            {
                Console.WriteLine("Json字符串转XmlDocument:");
                var document = XmlJsonConvertor.JsonConvertToXMLElement(jsonContent);
                Console.WriteLine(document.InnerXml);
            }

            JObject rootNode = new JObject();
            JArray jArray = new JArray();
            JObject jObject = new JObject();
            jObject.Add("任务名称", "任务1");
            jObject.Add("设备型号", "设备型号1");
            jObject.Add("设备名称", "设备名称1");
            jObject.Add("创建时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            jArray.Add(jObject);
            rootNode.Add("taskList", jArray);
            {

                //string jsonStr = $"[{rootNode.ToString()}]";
                string jObjectXmlStr = XmlJsonConvertor.JsonConvertToXMLStr(rootNode);
                Console.WriteLine("jObject转xml：");
                Console.WriteLine(jObjectXmlStr);
            }

            Console.WriteLine("jObject转XMLDocument:");
            XmlDocument result1 = XmlJsonConvertor.JsonConvertToXMLElement(rootNode);
            {
                JObject result2 = XmlJsonConvertor.XMLConvertToJObject(xmlDocument);
                Console.WriteLine("XmlDocument转jObject：");
                Console.WriteLine(result2.ToString());
            }
            {
                JObject result3 = XmlJsonConvertor.XMLConvertToJObject(xmlContent);
                Console.WriteLine(result3.ToString());

            }
        }
    }
}
